package com.easoft.code.pos.ui.models;

import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.event.ListDataListener;

/**
 *
 * @author Eliu
 * @param <T>
 */
public class PojoListModel<T> implements ComboBoxModel<T> {

    private List<T> data;
    private T selectedItem;

    public PojoListModel(List<T> data) {
        this.data = data;
    }

    @Override
    public int getSize() {
        return data.size();
    }

    @Override
    public T getElementAt(int index) {
        return data.get(index);
    }

    @Override
    public void addListDataListener(ListDataListener l) {
    }

    @Override
    public void removeListDataListener(ListDataListener l) {
    }

    @Override
    public void setSelectedItem(Object anItem) {
        for (T i : data) {
            if (i.equals(anItem)) {
                selectedItem = i;
                break;
            }
        }
    }

    @Override
    public Object getSelectedItem() {
        return selectedItem;
    }
}
